package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peppa.petshop.entity.Favorite;
import com.peppa.petshop.entity.Good;
import com.peppa.petshop.mapper.FavoriteMapper;
import com.peppa.petshop.mapper.GoodMapper;
import com.peppa.petshop.service.FavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.FavoriteVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements FavoriteService {

    @Autowired(required = false)
    private FavoriteMapper favoriteMapper;
    @Autowired(required = false)
    private GoodMapper goodMapper;

    @Override
    public List<FavoriteVO> getMyFavor(Integer customerId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer",customerId);
        List<Favorite> list = favoriteMapper.selectList(queryWrapper);
        List<FavoriteVO> favoriteVOS = new ArrayList<>();
        for (Favorite favorite : list) {
            FavoriteVO favoriteVO = new FavoriteVO();
            BeanUtils.copyProperties(favorite,favoriteVO);
            Good good = goodMapper.selectById(favorite.getGood());
            favoriteVO.setGoodId(favorite.getGood());
            favoriteVO.setGood(good.getGoodName());
            favoriteVOS.add(favoriteVO);
        }
        return favoriteVOS;
    }
}
